package cn.tedu.charging.user.controller;

import cn.tedu.charging.common.pojo.po.User;
import cn.tedu.charging.common.pojo.response.JsonResult;
import cn.tedu.charging.user.service.UserService;

import cn.tedu.charging.user.pojo.param.UserLoginParam;
import cn.tedu.charging.user.pojo.param.UserQuery;
import cn.tedu.charging.user.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user")
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    UserService userService;
    @ApiOperation("登录接口")
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam) {
        log.debug("登录接口入参为{}",userLoginParam);
        log.debug("登录接口调用业务层的入参是{}",userLoginParam);
        UserVO userVO =  userService.login(userLoginParam);
        JsonResult jsonResult = JsonResult.ok(userVO,"成功登录");
        log.debug("登录接口调用业务层的入参是{}出参是{}",userLoginParam,userVO);
        return jsonResult;
    }
    @ApiOperation("查询用户接口")
    @GetMapping("select")
    public JsonResult selectUser(UserQuery userQuery) {
    log.debug("查询用户接口的入参为{}",userQuery);
    log.debug("查询用户接口调用业务层的入参为{}",userQuery);
    List<UserVO> userVOList = userService.selectUser(userQuery);
        JsonResult jsonResult = JsonResult.ok(userVOList,"成功查找用户");
    log.debug("查询用户接口调用业务层的入参为{}出参为{}",userQuery,jsonResult);
    return jsonResult;
    }
    @ApiOperation("重置密码接口")
    @PostMapping("update/password/{userId}")
    public JsonResult resetPassword(@PathVariable Long userId) {
        log.debug("用户重置密码接口的入参为{}",userId);
        userService.resetPassword(userId);
        JsonResult jsonResult = JsonResult.ok("成功重置用户密码");
        log.debug("用户重置密码接口的入参为{}出参{}",userId,jsonResult);
        return jsonResult;
    }
    @ApiOperation("删除用户接口")
    @PostMapping("delete/{userId}")
    public JsonResult deleteUser(@PathVariable Long userId) {
        log.debug("删除用户接口的入参为{}",userId);
        userService.deleteUser(userId);
        JsonResult jsonResult = JsonResult.ok("成功删除用户");
        log.debug("删除用户接口的入参为{}出参为{}",userId,jsonResult);
        return jsonResult;
    }
    @ApiOperation("修改用户状态接口")
    @PostMapping("update/status/{userId}/{status}")
    public JsonResult updateStatus(@PathVariable Long userId,@PathVariable String status) {
        log.debug("修改用户状态接口的入参为{}{}",userId,status);
        userService.updateStatus(userId,status);
        JsonResult jsonResult = JsonResult.ok("成功修改用户状态");
        log.debug("修改用户状态接口的入参为{}{},出参为{}",userId,status,jsonResult);
        return jsonResult;
    }
    @ApiOperation("新增用户接口")
    @PostMapping("insert")
    public JsonResult insertUser(@RequestBody User user) {
        log.debug("新增用户接口的入参为{}",user);
        log.debug("新增用户接口调用业务层的入参为{}",user);
        userService.insertUser(user);
        JsonResult jsonResult = JsonResult.ok("插入成功");
        log.debug("新增用户接口调用业务层的入参为{}出参为{}",user,jsonResult);
        return jsonResult;

    }
    @ApiOperation("通过删除员工删除用户接口")
    @PostMapping("deleteByStaff/{tel}")
    public JsonResult deleteUserByStaff(@PathVariable String tel) {
        log.debug("通过员工删除用户接口的入参为{}",tel);
        userService.deleteUserByStaff(tel);
        JsonResult jsonResult = JsonResult.ok("成功删除用户");
        log.debug("删除用户接口的入参为{}出参为{}",tel,jsonResult);
        return jsonResult;
    }
}
